package com.sj.uabrowser.app;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.internal.Primitives;

import java.lang.reflect.Type;
import java.util.Map;

public class SPUtil {

    /**
     * 保存在手机data区的默认的文件名
     */
    public static final String FILE_NAME = "umbrowser_share_data";

    public static final String KEY_ACCESSTOKEN = "AccessToken";
    public static final String KEY_FIRSTUSE = "FirstUse";
    public static final String KEY_USERDATA = "UserData";
    public static final String KEY_LOCATIONDATA = "LocationData";
    public static final String KEY_FONTSIZE = "FontSize";
    public static final String KEY_NIGHTMODE="NightMode";
    public static final String KEY_DOWNLOAD_PATH= "Download";
    public static final String APPLINKS = "appLinks";
    public static final String APPSHARETITLE = "appShareTitle";
    public static final String APPSHARETEXT = "appShareText";
    public static final String APPSHAREIMG = "appShareImg";
    public static final String KEY_NO_IMAGE = "noimg";
    public static final String KEY_BDKEYS = "bdkeys";
    public static final String NAV_PARAM = "nav_param";
    public static final String KEY_PROVINCE = "province";

    public static final String KEY_DISTRICT = "district";
    public static final String KEY_BANNER = "bannerAD";
    public static final String KEY_INTERSTITIALAD = "interstitialAD";
    public static final String KEY_SPLASHAD = "splashAD";
    public static final String KEY_CONTENTAD1 = "contentAD1";//
    public static final String KEY_CONTENTAD2 = "contentAD2";//原生广告

    public static final String KEY_CITY = "city";
    public static final String KEY_CITYID = "cityid";
    public static final String PERMISSION_READ_PHONE_STATE ="read_phone_status_permission";
    public static final String PERMISSION_REQUEST_LOCATION = "request_location";
    public static final String BDVIDEO_FROMS = "bdvideo_froms";
    public static final String KEY_ISSHARED = "isshared";
    public static final String UUID = "uuid";
    public static final String KEY_NOTRACE = "notrace";
    private int bannerAD;
    private int interstitialAD;
    private int splashAD;
    private int contentAD1;
    private int contentAD2;

    public static void put(Context context, String key, String object) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, object);
        editor.commit();
    }

    public static void put(Context context, String key, boolean object) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, object);
        editor.commit();
    }


    public static void put(Context context,String key,int value){
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static void putObj(Context context, String key, Object object) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        Gson gson = new Gson();
        String json_obj = gson.toJson(object);
        editor.putString(key, json_obj);
        editor.commit();
    }


    /**
     * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
     */
    public static String get(Context context, String key, String defaultObject) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, defaultObject);
    }

    public static boolean get(Context context, String key, boolean defaultObject) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getBoolean(key, defaultObject);
    }

    public static int get(Context context, String key, int defaultObject) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defaultObject);
    }

    public static <T> T getObj(Context context, String key, Class<T> classOfT, String defaultObject) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String json_obj = sp.getString(key, defaultObject);
        if (json_obj.equals("")) {
            return null;
        } else {
            Gson gson = new Gson();
            Object object = gson.fromJson(json_obj, (Type) classOfT);
            return Primitives.wrap(classOfT).cast(object);
        }
    }


    /**
     * 移除某个key值已经对应的值
     */
    public static void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 清除所有数据
     */
    public static void clear(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.clear();
        editor.commit();
    }

    /**
     * 查询某个key是否已经存在
     */
    public static boolean contains(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.contains(key);
    }

    /**
     * 返回所有的键值对
     */
    public static Map<String, ?> getAll(Context context) {
        SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sp.getAll();
    }


}
